#ifndef CUSTOMSORT_H
#define	CUSTOMSORT_H
#include <iostream>

void merge(int* array, int lb, int split, int rb) {

	int pos1 = lb;
	int pos2 = split + 1;
	int pos3 = 0;

	int* temp = new int[rb - lb + 1];

	while(pos1 <= split && pos2 <= rb) {
		if(array[pos1] < array[pos2]) {
			temp[pos3++] = array[pos1++];
		} else {
			temp[pos3++] = array[pos2++];
		}
	}

	while(pos2 <= rb) {
		temp[pos3++] = array[pos2++];
	}

	while(pos1 <= split) {
		temp[pos3++] = array[pos1++];
	}

	for(pos3 = 0; pos3 < rb - lb + 1; ++pos3) {
		array[lb + pos3] = temp[pos3];
	}

	delete(temp);
}

void mergeSort(int* array, int lb,int rb) {

	int split;
	if(lb < rb) {

		split = (lb + rb) / 2;

		mergeSort(array, lb, split);
		mergeSort(array, split + 1, rb);
		merge(array, lb, split, rb);
	}
}

void sort(int* array, int size) {
	mergeSort(array,0,size);
}

bool isSorted(int* array, int size) {
	for(int i = 0; i < size - 1; ++i) {
		if(array[i] > array[i + 1]) {
			return false;
		}
	}
	return true;
}

void print(int* array, int size) {
	for(int i = 0; i < size; ++i) {
		printf("%d ", array[i]);
	}
}

void print(int* array, int size, char divisor){
	for (int i = 0; i < size;++i){
		printf("%d%c", array[i], divisor);
	}
}


#endif	/* CUSTOMSORT_H */
